
"""
封装获取cookie方法

"""

import requests
import urllib.request
import re
from Common import Log
from Conf import Config
from http import cookiejar
from urllib import request


class Cookies:
    def __init__(self):
        self.config = Config.Config()
        self.log = Log.MyLog()


    def get_authorization(self, env):
        """获取Cookie或鉴权"""
        headers = {
                "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko)\
                              Chrome/67.0.3396.99 Safari/537.36",
                "Content-Type": "application/x-www-form-urlencoded"
            }

        if env == "debug":
            login_url = 'https://' + self.config.loginHost_debug
            response = requests.post(login_url)
            cookie = cookiejar.CookieJar()
            handler = request.HTTPCookieProcessor(cookie)
            opener = request.build_opener(handler)
            response = opener.open(login_url)
            cookieValue = ''
            # print(cookieValue)
            for item in cookie:
                cookieValue += item.name + '=' + item.value + ';'
            headers['Cookie'] = cookieValue
            print(headers['Cookie'])
            response = requests.get(url=login_url)
            # print(cookieValue)
        elif env == "release":
            login_url = 'https://' + self.config.loginInfo_release
            response = requests.post(login_url)
            cookie = cookiejar.CookieJar()
            handler = request.HTTPCookieProcessor(cookie)
            opener = request.build_opener(handler)
            response = opener.open(login_url)
            cookieValue = ''
            # print(cookieValue)
            for item in cookie:
                cookieValue += item.name + '=' + item.value + ';'
            headers['Cookie'] = cookieValue
            # print(headers['Cookie'])
            response = requests.get(url=login_url)
        else:
            # print("get cookies error")
            self.log.error('get cookies error, please checkout!!!')


    # def get_session(self, env):
    #     """
    #     获取session
    #     :param env: 环境变量
    #     :return:
    #     """
    #     headers = {
    #         "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko)\
    #                       Chrome/67.0.3396.99 Safari/537.36",
    #         "Content-Type": "application/x-www-form-urlencoded"
    #     }
    #
    #     if env == "debug":
    #         login_url = 'http://' + self.config.loginHost_debug
    #         parm = self.config.loginInfo_debug
    #
    #         session_debug = requests.session()
    #         response = session_debug.post(login_url, parm, headers=headers)
    #         print(response.cookies)
    #         self.log.debug('cookies: %s' % response.cookies.get_dict())
    #         return response.cookies.get_dict()
    #
    #     elif env == "release":
    #         login_url = 'http://' + self.config.loginHost_release
    #         parm = self.config.loginInfo_release
    #
    #         session_release = requests.session()
    #         response = session_release.post(login_url, parm, headers=headers)
    #         print(response.cookies)
    #         self.log.debug('cookies: %s' % response.cookies.get_dict())
    #         return response.cookies.get_dict()
    #
    #     else:
    #         print("get cookies error")
    #         self.log.error('get cookies error, please checkout!!!')


if __name__ == '__main__':
    ss = Cookies()
    ss.get_authorization('debug')